<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of GiftController
 *
 * @author jack
 */
class GiftController extends ApiController {

    /**
     * @ApiDoc(
     *      description="赠送礼物"
     * )
     * @RequestParam(name="sendId", requirements="integer", description="发送人ID")
     * @RequestParam(name="receiveId", requirements="integer", description="接收人ID")
     * @RequestParam(name="gift", requirements="string", description="A,B,C,D对应四种礼物")
     * @RequestParam(name="quantity", requirements="integer", description="数量")
     * @RequestParam(name="message", requirements="string", description="留言")
     */
    public function sendAction() {
        $em = $this->getEntityManager();
        $request = $this->getRequest()->request;
        $gift = new \Wilead\IArtistBundle\Entity\Gift;
        $gift->setSend($em->getReference('Wilead\IArtistBundle\Entity\User', $request->get('sendId')));
        $gift->setReceive($em->getReference('Wilead\IArtistBundle\Entity\User', $request->get('receiveId')));
        $this->insertIntoEntity($request, $gift, array('gift', 'quantity', 'message'));

        $em->persist($gift);
        $em->flush();

        return $this->getFOSView()->setStatusCode(200)->setData('赠送成功');
    }

    /**
     * @ApiDoc(
     *      description="用户送出的礼物"
     * )
     */
    public function sendedAction($id) {
        return $this->getFOSView()->setData($this->getRepository('WileadIArtistBundle:Gift')->getGift($id, 'send'))->setStatusCode(200);
    }

    /**
     * @ApiDoc(
     *      description="用户收到的礼物"
     * )
     */
    public function receivedAction($id) {
        return $this->getFOSView()->setData($this->getRepository('WileadIArtistBundle:Gift')->getGift($id, 'receive'))->setStatusCode(200);
    }

    /**
     * @ApiDoc(
     *      description="删除礼物"
     * )
     * @RequestParam(name="id", requirements="integer", description="要删除的礼物ID")
     */
    public function deleteAction() {
        $gift = $this->getRepository('WileadIArtistBundle:Gift')->find($this->getRequest()->request->get('id'));

        if (!$gift) {
            return $this->getFOSView()->setData('无法找到指定礼物')->setStatusCode(404);
        }

        $em = $this->getEntityManager();
        $em->remove($gift);
        $em->flush();

        return $this->getFOSView()->setStatusCode(200)->setData('删除成功');
    }

}

?>
